/**
 * Copyright (C) 2008 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.wsgi.container.uri;

import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;

import cc.aileron.wsgi.WsgiHttpCategory;

/**
 * @author Aileron
 */
class WsgiUriTreeImpl implements WsgiUriTreeEntry
{

    /**
     * null-object
     */
    private static final HashMap<String, Integer> nullMap = new HashMap<String, Integer>();

    @Override
    public WsgiUriTreeEntry get(final String uriToken)
    {
        if (map.containsKey(uriToken) == false)
        {
            return tree;
        }
        return map.get(uriToken);
    }

    @Override
    public Map<String, Integer> getId(final WsgiHttpCategory method,
            final String extension)
    {
        final Map<String, Map<String, Integer>> oMap = idMap.get(method);
        if (oMap == null)
        {
            return nullMap;
        }
        return oMap.get(extension);
    }

    @Override
    public boolean isDynamic()
    {
        return dynamic;
    }

    @Override
    public void set(final String uriToken,
            final WsgiUriTreeEntry tree)
    {
        if (uriToken.indexOf('$') == 0)
        {
            this.tree = tree;
            this.tree.setDynamic(true);
        }
        else
        {
            map.put(uriToken, tree);
        }
    }

    @Override
    public void setDynamic(final boolean dynamic)
    {
        this.dynamic = dynamic;
    }

    @Override
    public void setId(final WsgiHttpCategory method,
            final String overrideKey,
            final String extension,
            final int id)
    {
        if (idMap.containsKey(method) == false)
        {
            idMap.put(method, new HashMap<String, Map<String, Integer>>());
        }
        if (idMap.get(method)
            .containsKey(extension) == false)
        {
            idMap.get(method)
                .put(extension, new HashMap<String, Integer>());
        }
        idMap.get(method)
            .get(extension)
            .put(overrideKey, id);
    }

    @Override
    public String toString()
    {
        return new ReflectionToStringBuilder(this).toString();
    }

    /**
     * constracto set root-uri name
     * 
     * @param name
     */
    public WsgiUriTreeImpl(final String name)
    {
        this.name = name;
    }

    /**
     * root-uri
     */
    public final String name;

    /**
     * id
     */
    private final EnumMap<WsgiHttpCategory, Map<String, Map<String, Integer>>> idMap = new EnumMap<WsgiHttpCategory, Map<String, Map<String, Integer>>>(
            WsgiHttpCategory.class);

    /**
     * root-map
     */
    private final HashMap<String, WsgiUriTreeEntry> map = new HashMap<String, WsgiUriTreeEntry>();

    /**
     * tree
     */
    private WsgiUriTreeEntry tree = null;

    /**
     * dynamic
     */
    private boolean dynamic;
}
